﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using DataContracts;
using System.ServiceModel;

namespace TrainingWorkshop.MVPWebsite.User.Views
{
    public class CreateNewUserPresenter : Presenter<ICreateNewUserView>
    {

        // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller
        //       The code will not work in the Shell module, as a module controller is not created by default
        //
        // private IUserController _controller;
        // public CreateNewUserPresenter([CreateNew] IUserController controller)
        // {
        // 		_controller = controller;
        // }


        private IUserController _usercontroller;
        public CreateNewUserPresenter([CreateNew] IUserController usercontroller)
        {
            _usercontroller = usercontroller;
        }
        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads
            View.Departments = _usercontroller.GetAllDepartment();
            View.Roles = _usercontroller.GetAllUserRole();
        }

        // TODO: Handle other view events and set state in the view

        public void OnAddNewUser()
        {
            UserDTO User = new UserDTO();
            User.FirstName = View.FirstName;
            User.LastName = View.LastName;
            DepartmentDTO Department = new DepartmentDTO();
            Department.DepartmentId = View.DepartmentId;
            Department.DepartmentName = View.DepartmentName;
            User.Department = Department;
            RoleDTO Role = new RoleDTO();
            Role.RoleId = View.RoleId;
            Role.RoleName = View.RoleName;
            StatusDTO Status = new StatusDTO();
            Status.StatusId = 1;
            Status.StatusName = "InUse";
            Role.Status = Status;
            User.Role = Role;

            
            try
            {
                _usercontroller.AddNewUser(User);
               
                View.SkipToAnotherPage("UserList.aspx");
               
            }
            catch (FaultException ex)
            {
                View.ShowOperationMessage(ex.Message);
                View.Roles = _usercontroller.GetAllUserRole();
            }
        }
    }
}




